package com.apesay.hbase.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;

public class ConfigUtils {

	private final static Logger log = LoggerFactory.getLogger(ConfigUtils.class);

	private static final String CONFIG_NAME = "hbase.properties";
	private static final Properties properties = new Properties();

	static {
		System.out.println("========ConfigUtils===========");
		InputStream in = ClassLoader.getSystemResourceAsStream(CONFIG_NAME);
		try {
			if(in==null){
				URL resource = ConfigUtils.class.getClassLoader().getResource(CONFIG_NAME);
				if (null!=resource){
					String filename = new URI(resource.toString()).toString();
					File file = new File(filename.replace("file:", ""));
					in = new FileInputStream(file);
					if (in!=null) {
						initMap(in);
					}
				}
			}else{
				initMap(in);
			}
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void initMap(InputStream in) throws IOException {
		properties.load(in);
	}

	public static String getValue(String key,String defalutValue) {
		return properties.getProperty(key,defalutValue);
	}

	public static Properties getProperties(){
		return properties;
	}
}
